// Dish
function Dish(id,name,price,icon){
	this.id = id;
	this.name = name;
	this.price = price;
	this.icon = icon;
}

Dish.prototype = {
	Constructor:Dish,
	getId:function(){
		return this.id;
	},
	setId:function(id){
		this.id = id;
	},
	getName:function(){
		return this.name;
	},
	setName:function(name){
		this.name = name;
	},
	getPrice:function(){
		return this.price;
	},
	setPrice:function(price){
		this.price = price;
	},
	getIcon:function(){
		return this.icon;
	},
	setIcon:function(icon){
		this.icon = icon;
	}
}

//Menu
function Menu(){
	this.dishs = new Array();
}

Menu.prototype = {
	Constructor:Menu,
	add:function(data){
		if(data != null){
			for(var i=0;i<data.length;i++){
				var dish = new Dish(data[i].mid, data[i].name, data[i].price, data[i].icon);
				this.dishs.push(dish);
			}
		}		
	},
	random:function(){
		var len = this.dishs.length-1;
		for(var i=0;i<len;i++){
			var p = Math.round(Math.random()*len);
			var temp = this.dishs[i];
			this.dishs[i] = this.dishs[p];
			this.dishs[p] = temp;
		}
	},
	getDishById:function(id){
		for(var i=0;i<this.dishs.length;i++){
			if(this.dishs[i].getId()==id){
				return this.dishs[i];
			}
		}
	}
}

//CartItem
function CartItem(dish){
	this.id = dish.getId();
	this.dish = dish;
	this.num = 1;
}

CartItem.prototype = {
	Constructor:CartItem,
	getId:function(){
		return this.id;
	},
	up:function(){
		this.num++;
	},
	down:function(){
		if(this.num>0){
			this.num--;
		}
	},
	getDish:function(){
		return this.dish;
	},
	getNum:function(){
		return this.num;
	}
}

//Cart
function Cart(){
	this.cartList = new Array();
}

Cart.prototype = {
	Constructor:Cart,
	add:function(dish){
		if(!this.isExit(dish)){
			var item = new CartItem(dish);
			this.cartList.push(item);
		}else{
			var index = this.getDishIndex(dish);
			this.cartList[index].up();
		}
	},
	size:function(){
		return this.cartList.length;
	}
	,
	getDishIndex:function(dish){
		for(var i=0;i<this.cartList.length;i++){
			if(this.cartList[i].id==dish.getId()){
				return i;
			}
		}
		return -1;
	},
	isExit:function(dish){
		var isTrue = false;
		for(var i=0;i<this.cartList.length;i++){
			if(this.cartList[i].id==dish.getId()){
				isTrue = true;
			}
		}
		return isTrue;
	},
	del:function(dish){
		var index = this.getDishIndex(dish);
		var num = this.cartList[index].num;
		if(num>0){
			this.cartList[index].down();
		}else{
			delete(this.cartList[index]);
		}
	},
	calculate:function(){
		var totalPrice = 0;
		for(var i=0;i<this.cartList.length;i++){
			if(this.cartList[i]){
				var price = this.cartList[i].getDish().getPrice();
				var num = this.cartList[i].getNum();
				totalPrice+=(price*num);
			}			
		}
		return totalPrice;
	},
	inbox:function(){
		return JSON.stringify(cart);
	},
	debox:function(jsonstr){
		if(jsonstr != ""){
			var result = JSON.parse(jsonstr);		
			for(var i=0;i<result.cartList.length;i++){
				var currentLine = result.cartList[i];
				var dish = new Dish(currentLine.dish.id, currentLine.dish.name, currentLine.dish.price, currentLine.dish.icon);
				var item = new CartItem(dish);
				item.num = currentLine.num;
				this.cartList.push(item);
			}
		}		
	},
	showorder:function(jsonstr){
		if(jsonstr != ""){
			var result = JSON.parse(jsonstr);		
			for(var i=0;i<result.cartList.length;i++){
				var currentLine = result.cartList[i];
				var dish = new Dish(currentLine.dish.id, currentLine.dish.name, currentLine.dish.price, currentLine.dish.icon);
				var item = new CartItem(dish);
				item.num = currentLine.num;
				this.cartList.push(item);
			}
		}
	}
}